<?php
/**
 * Created by PhpStorm.
 * User: 汪利东
 * Date: 2016/3/21
 * Time: 12:05
 */

namespace Admin\Controller;
use Think\Auth;
use Think\Controller;
use Service\ServiceContainer;

class BaseController extends Controller
{

    /**
     * BaseController constructor.
     */
    public function __construct()
    {

        parent::__construct();

        header('Content-Type:text/html;charset=utf-8');

        //开启模板
        layout('Public_layout');
        if(I('post.test')=='test'){
            return;
        }
        $noLogin=[
            'OrderPush'
        ];
        //无需登录的模块
        if(array_key_exists(CONTROLLER_NAME,$noLogin)){
            return;
        }
        //登录检查
        if (!session('?admin_user')) {
            $this->redirect('Login/index');
        }

        //
        $breadcrumb = CONTROLLER_NAME;

        //权限检查，控制到Controller
        $arr = M('AdminRule')->field('name,title')->select();
        foreach ($arr as $k => $v) {
            $ar[$k] = strtolower($v['name']);
            if (strtolower(CONTROLLER_NAME) == $ar[$k]) {
                $breadcrumb = $v['title'];
            }
            $rule[$v['name']] = $v['title'];
        }
        $this->assign('breadcrumb', $breadcrumb);
        //未加入到规则表的默认可访问
        if (in_array(strtolower(CONTROLLER_NAME), $ar)) {
            //超级管理员不限制
            if (session('admin_user.user_id') != '1') {
                $rule=M('admin_rule')->where(['name'=>CONTROLLER_NAME])->find();
                if($rule){
                    $rules=session('admin_user.rules');
                    $rules=explode(',',$rules['rules']);
                    if(!in_array($rule['id'],$rules)){
                        $this->error('没有权限访问本页面或执行该操作!');
                    }
                }
            }
        }

        $Md=[
            'Buy_index'=>['图书购买',1],
            'Buy_orderShow'=>['图书购买小票',6],
            'Apply_index'=>['活动报名',2],
            'Apply_orderShow'=>['活动报名小票打印',5],
            'Rent_index'=>['图书借阅',3],
            'Return_index'=>['图书归还',4],
            'Broken_index'=>['破损回收',5],
            'Broken_orderShow'=>['破损回收小票打印',2],
            'CardHandle_index'=>['会员办理',6],
            'CardDeposit_index'=>['会员充值',1],
            'CardDeposit_orderShow'=>['会员充值小票打印',4],
            'CardDelay_index'=>['办理延期',2],
            'CardFreeze_index'=>['挂失',3],
            'CardExchange_index'=>['换卡',4],
            'CardModify_index'=>['会员信息修改',5],
            'SearchBook_index'=>['图书查询',6],
            'SearchCard_index'=>['会员查询',1],
            'SearchSale_index'=>['销售查询',2],
            'SearchStock_index'=>['库存查询',3],
            'Refund_index'=>['退货款',4],
        ];
        $this->assign('T_title',$Md[CONTROLLER_NAME."_".ACTION_NAME][0]);
        $this->assign('T_type',$Md[CONTROLLER_NAME."_".ACTION_NAME][1]);

        $this->di=new ServiceContainer();
    }

    /**
     * 空操作
     */
    public function _empty()
    {
        layout(false);
//        $this->display('Public_404');
        $this->error('暂未开通，敬请期待','',1);
    }
}